﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StuRecruitSysDAL;
using System.Threading;
using StuRecruitSysBLL;
using StuRecruitSysModels;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.ComponentModel;
using System.Drawing;
using System.Text;


namespace StuRecruitSys
{

    public partial class StudentHome : System.Web.UI.Page
    {
        public String uname = StuRecruitSysDAL.cuser.uname;
        StudentService stus = new StudentService();
        RegisteManager regma = new RegisteManager();
        public  string iniSname;
        public  string iniManme ;
        public  string iniSex;
        private static string connString = "Data Source=.;Initial Catalog=StuRecruitDB;User ID=sa;Pwd=123";
   

        protected void Page_Load(object sender, EventArgs e)
        {

            //将图片路径取出并且显示在页面上
            string a = getPicName();
            this.HeadImage1.ImageUrl = a;
            this.HeadImage2.ImageUrl = a;
            bool ispayed = isPay();
            if (ispayed)
            {
                this.btnPay.Enabled = false;
                this.btnPay.CssClass = "btn btn-large btn-inverse right ";
                this.btnPay.Text = "您已经缴纳过学费";
            }else{

            }

                //完善学生基本信息
            username.Value = getStudentDetail().Username;
            sno.Value = getStudentDetail().Sno;
            iniSname = sname.Value;
            iniManme = mname.Text;
            iniSex = selectsex1.Text;
            sname.Value = getStudentDetail().Sname;
            mname.Text = getStudentDetail().Mname;
            selectsex1.Text = getStudentDetail().Sex;
          


            //查询学生详细资料表单内容
            lusername.Value = getStudentDetail().Username;
            lsno.Value = getStudentDetail().Sno;         
            lsname.Value = getStudentDetail().Sname;
            lmname.Text = getStudentDetail().Mname;
            lselectsex1.Text = getStudentDetail().Sex;
            lsmonney.Value = getStudentDetail().Smoney;
            lclass.Value = getStudentDetail().Sclass;
            ldorno.Value = getStudentDetail().Dormno;
            fee.Value = getStudentFee();
            smoney.Value = getStudentDetail().Smoney;
           

            //缴费页面
            TextFee.Value = getStudentFee();
            Textmoney.Value = getStudentDetail().Smoney;       
        }


        //获取学生详细信息
        public Student getStudentDetail()
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = connString;
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "select DISTINCT  * from student where  username='" + StuRecruitSysDAL.cuser.uname + "'";
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataTable dtt = new DataTable();
            sda.Fill(dtt);
            Student student = new Student();
            try
            {
                student.Username = dtt.Rows[0]["username"].ToString().Trim();
                student.Sno = dtt.Rows[0]["sno"].ToString().Trim();
                student.Sname = dtt.Rows[0]["sname"].ToString().Trim();
                student.Mname = dtt.Rows[0]["mname"].ToString().Trim();
                student.Sex = dtt.Rows[0]["sex"].ToString().Trim();
                student.Smoney = dtt.Rows[0]["smoney"].ToString().Trim();
                student.Sclass = dtt.Rows[0]["sclass"].ToString().Trim();
                student.Dormno = dtt.Rows[0]["dormno"].ToString().Trim();
                con.Close();
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('对不起，没有相关的专业信息!');</script>");
            }
            return student;
        }
        public string getStudentFee()
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = connString;
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "select DISTINCT * from v_StudentDetail where  username='" + StuRecruitSysDAL.cuser.uname + "'";
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataTable dtt = new DataTable();
            sda.Fill(dtt);
            string Tmoney = null ;
            try
            {
                Tmoney = dtt.Rows[0]["tmoney"].ToString().Trim();
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('对不起，没有相关的专业费用记录!');</script>");
            }
          
            con.Close();      
            return Tmoney;

        }

        //判断是否已经缴纳过学费
        protected bool isPay()
        {
            bool ispayed = false;

            SqlConnection con = new SqlConnection();
            con.ConnectionString = connString;
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "select DISTINCT * from financedept where  sno='" + getStudentDetail().Sno + "'";
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataTable dtt = new DataTable();
            sda.Fill(dtt);
            string record = null;
            try
            {
                record = dtt.Rows[0]["record"].ToString().Trim();
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('对不起，没有相关的付费记录!');</script>");
            }
            if (record != "该同学还未缴纳学费")
            {
                ispayed = true;
            }
            con.Close();

            return ispayed;
        }
        public String getPicName()
        {
            string picName = "min张三.jpg";
            
            SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=123;database=StuRecruitDB");
            // 查询用sql语句
            string sql = string.Format(
                "SELECT *  FROM  student  WHERE  username='{0}' ", StuRecruitSysDAL.cuser.uname);
            SqlCommand command = new SqlCommand(sql, con);
            con.Open();
            SqlDataReader objReader = command.ExecuteReader(CommandBehavior.CloseConnection);
            try
            {
                if (objReader.Read())
                {
                    picName = Convert.ToString(objReader["picpath"]);
                }
            }
            finally
            {
                con.Close(); // 关闭数据库连接
            }
            if (picName == null)
            {
                picName = "min张三.jpg";
                return picName;
            }
            return picName;
        }


        protected void detail_onclick(object sender, EventArgs e)
        {
            Student stu = new Student();
            stu.Username = this.username.Value.Trim();
            stu.Sno = this.sno.Value;
            stu.Sname = iniSname;
            stu.Mname = iniManme;
            stu.Sex = iniSex;
       

            if (stu.Sname == "")
            {

                if (!Page.ClientScript.IsStartupScriptRegistered("test"))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('请填写真实姓名!');</script>");
                }

                return;
            }
            if (stu.Mname == "")
            {
                if (!Page.ClientScript.IsStartupScriptRegistered("test"))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('请选择您的专业!');</script>");
                }
                return;
            }
            if (stu.Sex == "")
            {
                if (!Page.ClientScript.IsStartupScriptRegistered("test"))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('请选择您的性别!');</script>");
                }
                return;
            }
            else
            {
                try
                {
                    regma.RegisteDetail(stu);
                   Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('您已经成功提交详细信息!');</script>");
                }
                catch
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('详细信息提交失败!');</script>");
                }
                
            }

        }

        
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string fullpath;
            if (this.FileUpload1.FileName != "")
            {
                if (this.FileUpload1.FileName != "")
                {
                    this.FileUpload1.SaveAs(Server.MapPath("img/") + this.FileUpload1.FileName);
                    fullpath = Server.MapPath("img/") + this.FileUpload1.FileName;
                    string picPath = "img/"+this.FileUpload1.FileName;

                    //将图片路径存入数据库
                    SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=123;database=StuRecruitDB");
                    string sql = string.Format("update student set picpath='{0}'where username='{1}'", picPath, StuRecruitSysDAL.cuser.uname);
                    try
                    {
                        SqlCommand command1 = new SqlCommand(sql, con);  // 创建command对象 
                        con.Open();  // 打开数据库连接
                        int result1 = command1.ExecuteNonQuery();  // 执行命令
                        if (result1 < 1)
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('上传头像失败');</script>");
                        }
                        else
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('上传头像成功');</script>");
                        }
                    }
                    finally
                    {
                        con.Close(); // 关闭数据库连接
                    }

                }
            }
        }

        protected void btnPay_onclick(object sender, EventArgs e)
        {

            TextFee.Value = getStudentFee();
            Textmoney.Value = getStudentDetail().Smoney;
            int fee = int.Parse(getStudentFee());
            int mymoney = int.Parse(getStudentDetail().Smoney);
            string sno = getStudentDetail().Sno;
            string record = uname + DateTime.Now.ToLocalTime().ToString()+"缴纳学费" + fee+"元";
            if (fee > mymoney)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('您的账户资金余额不足，请及时充值再进行缴费！');</script>");

            }else{
                int restMoney = mymoney - fee;
                SqlConnection conn = new SqlConnection(connString);
                string sql = string.Format("update student set smoney='{0}' where username='{1}'", restMoney, StuRecruitSysDAL.cuser.uname);
                string sql1 = string.Format("update financedept set fmoney=fmoney+'{0}',sfee='{1}',record='{2}' where sno='{3}'", fee,fee, record, sno);
                try
                {
                    SqlCommand command1 = new SqlCommand(sql, conn);  // 创建command对象 
                    SqlCommand command2 = new SqlCommand(sql1, conn);  // 创建command对象 
                    conn.Open();  // 打开数据库连接
                    int result1 = command1.ExecuteNonQuery();  // 执行命令
                    int result2 = command2.ExecuteNonQuery();  // 执行命令
                    if (result1 < 1 || result2 < 1)
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('缴费失败，请重试！');</script>");
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>alert('恭喜您已缴费成功，请等待出纳确认！');</script>");
                    }
                }

                finally
                {
                    conn.Close();  // 关闭数据库连接
                }
            }
        }
    }
}